package com.sf.bank.controller;

import com.sf.bank.entity.UserInfo;
import com.sf.bank.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;

@Controller
//@RestController = @Controller+@ResponseBody
@RequestMapping("user")
/**
 * 做登陆处理
 */
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("list")
    public List<UserInfo> queryUserAll() {
        List<UserInfo> list = userService.findAll();
        return list;
    }
    @RequestMapping("list/{name}")
    public List<UserInfo> queryUserAll(@PathVariable String name) {
        List<UserInfo> list = userService.queryUserByName(name);
        return list;
    }
    @RequestMapping("listTest")
    public String listTest() {
        return "list";
    }
    @RequestMapping("insertUserInfo")
    public int insertUserInfo(@ModelAttribute UserInfo userInfo) {
        return userService.insertUserInfo(userInfo);
    }

    /**
     * 这个方法做用户登陆，有能力看看token怎么处理登陆
     * 自己写service，serviceimpl，dao，mapper，有例子
     * tijiaoceshidemo1
     */
    @RequestMapping("userAuthLogin")
    //返回页面标签
    @ResponseBody
    public ModelAndView userAuthLogin(UserInfo userInfo, Model model){
        //去数据库检查有没有这个用户
        List<UserInfo> list = userService.userAuthLogin(userInfo);
        if(list.size()>0){
            return new ModelAndView("main");
        }
        model.addAttribute("errorMsg", "用户名或密码错误");
//        return "userLogin";
        return new ModelAndView("userLogin");
    }

    /**
     * 跳转到登陆页面
     */
    @RequestMapping("/toUserLogin")
    public ModelAndView toUserLogin(HashMap<String, Object> map){
        map.put("hello", "欢迎进入HTML页面");
        System.out.println("跳转到登陆页面了");
//        List<Menu> list = new ArrayList<>();
//        list.add(new Menu("用户管理", "/users"));
//        model.addAttribute("list", list);
        return new ModelAndView("userLogin");
//        return "userLogin";
    }
}
